<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 内容信息
 */
namespace app\api\controller;

use  Mscore\Core\Controller;
use  app\api\model\Article as ArticleModel;
use  app\api\model\Login as LoginModel;
use  Mscore\Core\Lang;

class ArticleController extends Controller{

    private $model;
    private $login;
    private $header;
    private $error;
    private $success; 

    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->model = new ArticleModel;
        $this->header =get_all_headers();
        $this->header['language']=isset($this->header['language'])&&!empty($this->header['language'])?$this->header['language']:config('langs.default');
        Lang::init($this->header['language']);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }   

    /**
     * 类别列表
     * @param array $cate 
     * @param array $where 
     */ 
    public function cate(){  
        switch (true) {
            case !empty(post('id')):
                $where = 'a.id='.post('id');
                $datalist = $this->model->cate($where);
                if(!empty($datalist['data'][0]['titles'])){
                    $datalist['data'][0]['title']=isset(json_decode($datalist['data'][0]['titles'],true)[$this->header['language']])?json_decode($datalist['data'][0]['titles'],true)[$this->header['language']]:$datalist['data'][0]['title'];
                    unset($datalist['data'][0]['titles']);
                }
                if(!empty($datalist['data'][0]['introduction'])){
                    $datalist['data'][0]['introduction']=isset(json_decode($datalist['data'][0]['introduction'],true)[$this->header['language']])?json_decode($datalist['data'][0]['introduction'],true)[$this->header['language']]:'';
                }
                $this->success['data']=$datalist['data'];
                $this->success['count']=$datalist['total'];
                $this->echojson($this->success);
                break;
            default:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = ' a.language = "'.$this->header['language'].'" ';
                if(post('title')){
                    $where .= ' and a.title like "%'.post('title').'%"';
                }
                if(post('acode')){
                    $where .= ' and a.acode like "%'.post('acode').'%"';
                }
                $datalist = $this->model->cate($where,$page,$limit);
                foreach ($datalist['data'] as $key => $value) {
                    if(!empty($value['titles'])){
                        $datalist['data'][$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];
                    }
                    if(!empty($value['introduction'])){
                        $datalist['data'][$key]['introduction']=isset(json_decode($value['introduction'],true)[$this->header['language']])?json_decode($value['introduction'],true)[$this->header['language']]:'';  
                    }
                    unset($datalist['data'][$key]['titles']);
                }
                $this->success['data']=$datalist['data'];
                $this->success['count']=$datalist['total'];
                $this->echojson($this->success);
                break;
        }
    }

    /**
     * 文章列表
     * @param array $select 
     * @param array $where 
     */ 
    public function select(){
        switch (true) {
            case !empty(post('id')):
                $where = 'a.id='.post('id');
                $datalist = $this->model->select($where);
                if(!empty($datalist['data'][0]['titles'])){
                    $datalist['data'][0]['title']=isset(json_decode($datalist['data'][0]['titles'],true)[$this->header['language']])?json_decode($datalist['data'][0]['titles'],true)[$this->header['language']]:$datalist['data'][0]['title'];
                    unset($datalist['data'][0]['titles']);
                }
                if(!empty($datalist['data'][0]['introduction'])){
                    $datalist['data'][0]['introduction']=isset(json_decode($datalist['data'][0]['introduction'],true)[$this->header['language']])?json_decode($datalist['data'][0]['introduction'],true)[$this->header['language']]:'';
                }
                if(!empty($datalist['data'][0]['content'])){
                    $datalist['data'][0]['content']=isset(json_decode($datalist['data'][0]['content'],true)[$this->header['language']])?htmlspecialchars_decode(json_decode($datalist['data'][0]['content'],true)[$this->header['language']]):'';
                }
                $this->success['data']=$datalist['data'];
                $this->success['count']=$datalist['total'];
                $this->echojson($this->success);
                break;
            default:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = ' a.dels<1 and a.status >0 ';
                if(post('title')){
                    $where .= ' and a.title like "%'.post('title').'%"';
                }
                if(post('acode')){
                    $where .= ' and a.acode like "%'.post('acode').'%"';
                }
                $file='a.id, a.title, a.titles, a.cid,a.introduction, a.keywords, a.description, a.content,a.imgurl,a.is_open,a.url,a.open_url,a.createtime';
                $datalist = $this->model->select($where,$page,$limit,$file);
                foreach ($datalist['data'] as $key => $value) {
                    if(!empty($value['titles'])){
                        $datalist['data'][$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];
                    }
                    if(!empty($value['introduction'])){
                        $datalist['data'][$key]['introduction']=isset(json_decode($value['introduction'],true)[$this->header['language']])?json_decode($value['introduction'],true)[$this->header['language']]:'';  
                    }
                    if(!empty($value['content'])){
                        $datalist['data'][$key]['content']=isset(json_decode($value['content'],true)[$this->header['language']])?htmlspecialchars_decode(json_decode($value['content'],true)[$this->header['language']]):'';  
                    }
                    unset($datalist['data'][$key]['titles']);
                }
                $this->success['data']=$datalist['data'];
                $this->success['count']=$datalist['total'];
                $this->echojson($this->success);
                break;
        } 
    }

    /**
     * 文章列表
     * @param array $indexlist 
     * @param array $where 
     */ 
    public function indexlist(){
        $num=post('num');
        $page = 1;
        $limit = !empty($num)?$num:4;
        $where = ' a.dels<1 and a.status >0 and a.is_index>1 ';
        if(post('acode')){
            $where .= ' and a.acode = "'.post('acode').'"';
        }
        $file='a.id, a.title, a.titles, a.cid,a.introduction,a.imgurl,a.is_open,a.url,a.open_url,a.is_index';
        $datalist = $this->model->select($where,$page,$limit,$file);
        foreach ($datalist['data'] as $key => $value) {
            if(!empty($value['titles'])){
                $datalist['data'][$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];
            }
            if(!empty($value['introduction'])){
                $datalist['data'][$key]['introduction']=isset(json_decode($value['introduction'],true)[$this->header['language']])?json_decode($value['introduction'],true)[$this->header['language']]:'';  
            }
            unset($datalist['data'][$key]['titles']);
        }
        $this->success['data']=$datalist['data'];
        $this->echojson($this->success);
    }
     
}
